﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using BUS;
using DTO;

namespace WebRaoVat
{
    public partial class Site : System.Web.UI.MasterPage
    {
        public List<DanhMucChinhDTO> danhMucChinh = new List<DanhMucChinhDTO>();
        public List<List<DanhMucConDTO>> danhMucCon = new List<List<DanhMucConDTO>>();
        public List<QuangCaoDTO> quangCao = new List<QuangCaoDTO>();
        public int _soQuangCao = 9;
		public int SoTinNhanChuaDoc = 0;
        //public SqlDataSource dataSourceDiaDiem = new SqlDataSource();
        public LogoDTO logo;

        public string backGround = "background-image: url('../images/bido.jpg')";
        public string slogant1 = "Rao vặt toàn cầu";
        public string slogant2 = "Rao tin mua bán tất cả các mặt hàng";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["danhMucChinh"] == null || true)
                LoadResource(Server.MapPath("Gui.xml"));
            else
            {
                danhMucChinh = (List<DanhMucChinhDTO>)Session["danhMucChinh"];
                danhMucCon = (List<List<DanhMucConDTO>>)Session["danhMucCon"];
                quangCao = (List<QuangCaoDTO>)Session["quangCao"];
            }
            _soQuangCao = quangCao.Count;

            //Hiện số tin nhắn chưa đọc
            SoTinNhanChuaDoc = 0;

            if (Session["NguoiDungDangDangNhap"] != null && Session["NguoiDungDangDangNhap"].ToString().Length > 0)
            {
                int MaNguoiDung = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString()).MaNguoiDung;
                SoTinNhanChuaDoc = TinNhanBUS.LaySoTinNhanChuaDoc(MaNguoiDung);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        private void LoadResource(string xmlFilePath)
        {
            XmlDocument taiLieu = null;
            try
            {
                taiLieu = XL_XML.ReadDocumentFile(xmlFilePath);
            }
            catch { }
            if (taiLieu == null)
                return;

            LoadMenu(taiLieu);
            LoadQuangCao(taiLieu);
            LoadLogo(taiLieu);
            LoadAnhNen(taiLieu);
            LoadSlogant(taiLieu);

            //dataSourceDiaDiem = (SqlDataSource)DiaDiemBUS.getTenAll();
        }

        private void LoadMenu(XmlDocument taiLieu)
        {
            XmlNodeList dsDanhMucChinhNode = XL_XML.ReadNodeList(taiLieu, "/GIAODIEN/MENU/DANHMUCCHINH");

            List<DanhMucConDTO> listDanhMucConInChinh;
            XmlNodeList dsDanhMucConInChinhNode;

            foreach (var danhMucItem in dsDanhMucChinhNode)
            {
                danhMucChinh.Add(new DanhMucChinhDTO(int.Parse(((XmlElement)danhMucItem).GetAttribute("ma")),
                    ((XmlElement)danhMucItem).GetAttribute("ten")));

                dsDanhMucConInChinhNode = XL_XML.ReadNodeList(taiLieu, "/GIAODIEN/MENU/DANHMUCCHINH[@ma='" +
                    ((XmlElement)danhMucItem).GetAttribute("ma") + "']/DANHMUCCON");
                listDanhMucConInChinh = new List<DanhMucConDTO>();

                foreach (var danhMucConItem in dsDanhMucConInChinhNode)
                {
                    listDanhMucConInChinh.Add(new DanhMucConDTO(int.Parse(((XmlElement)danhMucConItem).GetAttribute("ma")),
                        ((XmlElement)danhMucConItem).GetAttribute("ten")));
                }

                danhMucCon.Add(listDanhMucConInChinh);
            }

            Session["danhMucChinh"] = danhMucChinh;
            Session["danhMucCon"] = danhMucCon;
        }

        private void LoadQuangCao(XmlDocument taiLieu)
        {
            XmlNodeList dsQuangCaoNode = XL_XML.ReadNodeList(taiLieu, "/GIAODIEN/BLOCKRIGHT/QUANGCAO");
            foreach (var quangCaoItem in dsQuangCaoNode)
            {
                quangCao.Add(new QuangCaoDTO(int.Parse(((XmlElement)quangCaoItem).GetAttribute("ma")),
                    ((XmlElement)quangCaoItem).GetAttribute("link"),
                    ((XmlElement)quangCaoItem).GetAttribute("resource")));
            }
            Session["quangCao"] = quangCao;
        }

        private void LoadLogo(XmlDocument taiLieu)
        {
            XmlNodeList logoS = XL_XML.ReadNodeList(taiLieu, "/GIAODIEN/LOGO");
            logo = new LogoDTO();
            logo.DuongDanLogo = ((XmlElement)logoS[0]).GetAttribute("duongdan");
            if (logo.DuongDanLogo.Equals(""))
                logo.DuongDanLogo = "images/logo.gif";
            Session["logo"] = logo;
        }

        private void LoadAnhNen(XmlDocument taiLieu)
        {
            XmlNodeList logoS = XL_XML.ReadNodeList(taiLieu, "/GIAODIEN/ANH_NEN");
            backGround = ((XmlElement)logoS[0]).GetAttribute("duongdan");
            backGround = "background-image: url('../" + backGround + "')";
            if (backGround.Equals(""))
                backGround = "background-image: url('../images/bido.jpg')";
            Session["anhnen"] = backGround;
        }

        private void LoadSlogant(XmlDocument taiLieu)
        {
            XmlNodeList logoS = XL_XML.ReadNodeList(taiLieu, "/GIAODIEN/SLOGANT");
            slogant1 = ((XmlElement)logoS[0]).GetAttribute("one");
            slogant2 = ((XmlElement)logoS[0]).GetAttribute("true");
            if (slogant1 == "")
                slogant1 = "Rao vặt toàn cầu";
            if (slogant2 == "")
                slogant2 = "Rao tin mua bán tất cả các mặt hàng";
        }


        protected void lbtnThoat_Click(object sender, EventArgs e)
        {
            bool bKiemTra = false;
            NguoiDungDTO NguoiDungDangDangNhap = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString());

            if (Session["TrangHienTai"].ToString().Equals("NguoiDung.aspx?MaNguoiDung=" + NguoiDungDangDangNhap.MaNguoiDung.ToString()))
            {
                bKiemTra = true;
            }
            else
            {
                bKiemTra = false;
            }

            Session["NguoiDungDangDangNhap"] = null;
            Session["LaAdmin"] = null;

            if (bKiemTra == true ||
                Session["TrangHienTai"].ToString().CompareTo("DangBai.aspx") == 0)
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void lbtnNguoiDung_Click(object sender, EventArgs e)
        {
            Session["LoaiXemNguoiDung"] = "1";
            NguoiDungDTO nguoiDungHienTai = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString());

            if (nguoiDungHienTai != null)
            {
                if (nguoiDungHienTai.MaLoaiNguoiDung == 1 || nguoiDungHienTai.MaLoaiNguoiDung == 2)
                {
                    Response.Redirect("NguoiDung.aspx?MaNguoiDung=" + nguoiDungHienTai.MaNguoiDung.ToString());
                }
                else if (nguoiDungHienTai.MaLoaiNguoiDung == 3)
                {
                    Session["LaAdmin"] = "Admin";
                    Response.Redirect("Admin/quanlychuyenmuc.aspx");
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text.Trim().Length <= 0)
            {
                ShowMessage("Bạn chưa nhập thông tin tìm kiếm !");
            }
            else
            {
                Session["LoaiTimKiem"] = 1;
                Session["ChuoiTimKiem"] = txtSearch.Text.Trim().ToLower();
                Response.Redirect("TimKiem.aspx?MaTimKiem=-1");
            }
        }

        protected void lbtnDangBai_Click(object sender, EventArgs e)
        {
            //int a = Config._soLan;

            try
            {
                string nguoiDungHienTai = Session["NguoiDungDangDangNhap"].ToString();
                Response.Redirect("DangBai.aspx");
            }
            catch (System.Exception ex)
            {
                ShowMessage("Bạn chưa đăng nhập nên không có quyền đăng bài !");
            }
        }

        private void ShowMessage(string mess)
        {
            string strBuilder = "<script language='javascript'>alert('" + mess + "')</script>";
            Response.Write(strBuilder);
        }

        protected void lbtnTinNhan_Click(object sender, EventArgs e)
        {
            Session["LoaiXemNguoiDung"] = "1";
            NguoiDungDTO nguoiDungHienTai = NguoiDungBUS.layNguoiDungTheoTen(Session["NguoiDungDangDangNhap"].ToString());

            if (nguoiDungHienTai != null)
            {
                if (nguoiDungHienTai.MaLoaiNguoiDung == 2 || nguoiDungHienTai.MaLoaiNguoiDung == 3)
                {
                    Response.Redirect("NguoiDung.aspx?MaNguoiDung=" + nguoiDungHienTai.MaNguoiDung.ToString());
                }
            }
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList2.Text == "Tiếng Việt")
                Config._MANGONNGU = 1;
            else
                Config._MANGONNGU = 2;
        }
    }
}
